package com.example.common.domain.pojo.Deserialize;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;


// 自定义反序列化器示例
public class ListToStringDeserializer extends JsonDeserializer<String> {
    private static final ObjectMapper objectMapper = new ObjectMapper();

//    @Override
//    public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
//        Object options = objectMapper.readValue(p, Object.class);
//        return objectMapper.writeValueAsString(options);
//    }

    @Override
    public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        if (p.getCurrentToken() == JsonToken.START_ARRAY) {
            // 直接读取整个数组为字符串
            return p.getCodec().readTree(p).toString();
        } else {
            return p.getValueAsString();
        }
    }
}

